package com.incross.dawintest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.incross.dawin.DawinAdActivity;
import com.incross.dawin.DawinConfig;
import com.incross.dawin.util.BKLog;
import com.incross.dawin.views.BKDialog;
import com.incross.dawin.views.DawinAdPlayer;
import com.incross.dawin.views.DawinAdPlayer.OnPlayFinishListener;

public class AdActivity extends Activity implements OnPlayFinishListener {
	
	
	
	private Context mContext;
	private DawinAdPlayer mPlayer;
	private LinearLayout mLayout, listArea;
	
	private int mAdMode;
	
	private String mXml;
	
	private String mGcd, mMcd, mPcd, mScd, mCcd;
	private int mRepeatCount;
	private int mPlayCount = 0;
	
	private boolean mIsNextAdAvailable = true;
	
	private int mServerType;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		setContentView(R.layout.activity_ad);
		mContext = this;
		initViews();
		
		Intent intent = getIntent();
		mGcd = intent.getStringExtra("gcd");
		mMcd = intent.getStringExtra("mcd");
		mPcd = intent.getStringExtra("pcd");
		mScd = intent.getStringExtra("scd");
		mCcd = intent.getStringExtra("ccd");
		mRepeatCount = Integer.parseInt(intent.getStringExtra("repeat"));
		mServerType = intent.getIntExtra("server_type", DawinConfig.DAWIN_SERVER_REAL);
		
		if(mGcd == null || mMcd == null || mPcd == null || mScd == null || mCcd == null) {
			finishAdActivity(DawinAdPlayer.AD_FINISH_OTHER_ERROR);
		}
		BKLog.i("Received codes\nmcd:" + mMcd + "\ngcd:" + mGcd + "\npcd:" + mPcd + "\nscd:" + mScd + "\nccd:" + mCcd + "\nrepeat count:" + mRepeatCount);
		mPlayer.setDawinServerType(mServerType);
		mPlayer.showAd(mGcd, mMcd, mPcd, mScd, mCcd);
		BKLog.i("AD1 play");
	}
	
	
	@Override
	public void onConfigurationChanged (Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		BKLog.i("-------------- > onConfiguration changed");
		if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			if(listArea != null) {
				listArea.setVisibility(View.VISIBLE);
			}
		} else {
			if(listArea != null) {
				listArea.setVisibility(View.GONE);
			}
		}
	}
	
	
	@Override
	public void onResume() {
		if(mPlayer != null) {
			mPlayer.onResume();
		}
		super.onResume();
		
		BKLog.e("=====================================================================================");
		BKLog.e("onResume");
		BKLog.e("=====================================================================================");
		
	}
	
	@Override
	public void onPause() {
		if(mPlayer != null) {
			mPlayer.onPause();
		}
		super.onPause();
		
	}
	
	@Override
	public void onDestroy() {
		if(mPlayer != null) {
			mPlayer.onDestroy();
		}
		super.onDestroy();
	}
	
	public void initViews() {
		mPlayer = (DawinAdPlayer) findViewById(R.id.dap_ad_player);
		mPlayer.setOnPlayFinishListener(this);
//		mLayout = (LinearLayout) findViewById(R.id.ll_main);
//		mPlayer = new DawinAdPlayer(mContext);
//		mPlayer.setOnPlayFinishListener(this);
//		
//		LinearLayout.LayoutParams mainParam = 
//			new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//		mLayout.addView(mPlayer, mainParam);
//		
//		listArea = (LinearLayout) findViewById(R.id.ll_listArea);
		
	}

	
	@Override
	public void onPlayFinished(DawinAdPlayer dawinPlayer, int finishedBy) {
		mXml = mPlayer.getXmlString();
		BKLog.d("onPlayFinished : " + finishedBy);
		// 20140306 광고 반복 실행 테스트 코드 추가
		if(finishedBy != DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK &&
			finishedBy != DawinAdPlayer.AD_FINISH_BY_SNS_SHARE &&
			finishedBy != DawinAdPlayer.AD_FINISH_BY_TICKER) mPlayCount++;
		
		if(mRepeatCount == mPlayCount) {
			mIsNextAdAvailable = false;
		}
		
		if(mIsNextAdAvailable && 
			finishedBy != DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK &&
			finishedBy != DawinAdPlayer.AD_FINISH_BY_SNS_SHARE &&
			finishedBy != DawinAdPlayer.AD_FINISH_BY_TICKER) {
			BKLog.i("AD" + mPlayCount + " play");
			mPlayer.showAd(mGcd, mMcd, mPcd, mScd, mCcd);
		} else {
			switch(finishedBy) {
				case DawinAdPlayer.AD_FINISH_ACTION_COMPLETE:
					finishAdActivity(DawinAdPlayer.AD_FINISH_ACTION_COMPLETE);
					break;
					
				case DawinAdPlayer.AD_FINISH_ACTION_SKIP:
					finishAdActivity(DawinAdPlayer.AD_FINISH_ACTION_SKIP);
					break;
					
				case DawinAdPlayer.AD_FINISH_SCREEN_CLICK:
					finishAdActivity(DawinAdPlayer.AD_FINISH_SCREEN_CLICK);
					break;
					
				case DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK:
					Toast.makeText(mContext, "Action button click!", Toast.LENGTH_SHORT).show();
					break;
					
				case DawinAdPlayer.AD_FINISH_CALL_ACTION:
					Toast.makeText(mContext, "ad finish by call action", Toast.LENGTH_SHORT).show();
					break;
					
				case DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD:
					finishAdActivity(DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD);
					break;
					
				case DawinAdPlayer.AD_FINISH_BACK_KEY_PRESSED:
					finishAdActivity(DawinAdPlayer.AD_FINISH_BACK_KEY_PRESSED);
					break;
					
				case DawinAdPlayer.AD_FINISH_AFTER_ACTION:
					finishAdActivity(DawinAdPlayer.AD_FINISH_AFTER_ACTION);
					break;
					
				case DawinAdPlayer.AD_FINISH_BY_SNS_SHARE:
					Toast.makeText(mContext, "SNS Share button click!", Toast.LENGTH_SHORT).show();
					break;
					
				case DawinAdPlayer.AD_FINISH_AFTER_SNS_SHARE:
					finishAdActivity(DawinAdPlayer.AD_FINISH_AFTER_SNS_SHARE);
					break;

				case DawinAdPlayer.AD_FINISH_BY_TICKER:
					Toast.makeText(mContext, "Ticker Button click", Toast.LENGTH_SHORT).show();
					break;

				case DawinAdPlayer.AD_FINISH_AFTER_TICKER:
					finishAdActivity(DawinAdPlayer.AD_FINISH_AFTER_TICKER);
					break;
					
				case DawinAdPlayer.AD_FINISH_OTHER_REASON:
					finishAdActivity(DawinAdPlayer.AD_FINISH_OTHER_REASON);
					break;
					
				case DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED:
					finishAdActivity(DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED);
					break;
					
				case DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL:
					finishAdActivity(DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL);
					break;
					
				case DawinAdPlayer.AD_FINISH_OTHER_ERROR:
					finishAdActivity(DawinAdPlayer.AD_FINISH_OTHER_ERROR);
					break;
			}
		}
		
		
		
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		BKLog.e("=====================================================================================");
		BKLog.e("onActivityResult, reqcode : " + requestCode + ", resultCode : " + resultCode);
		BKLog.e("=====================================================================================");
		
		mXml = mPlayer.getXmlString();
		if(resultCode == RESULT_OK && requestCode == BKDialog.ACTION_CLICKVIEWER) {
			BKLog.e("Finish!!!!!!!!!!!!");
			finishAdActivity(DawinAdPlayer.AD_FINISH_AFTER_ACTION);
			
			if(data != null) {
				int rCode = data.getIntExtra(DawinAdActivity.KEY_RESULT_CODE, -1);
				if(rCode != -1) {
					switch(rCode) {
						case DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK:
							finishAdActivity(DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK);
							break;

						case DawinAdPlayer.AD_FINISH_SCREEN_CLICK:
							finishAdActivity(DawinAdPlayer.AD_FINISH_SCREEN_CLICK);
							break;

						case DawinAdPlayer.AD_FINISH_CALL_ACTION:
							finishAdActivity(DawinAdPlayer.AD_FINISH_CALL_ACTION);
							break;
					}
				}
			}
		}
	}
	
	public void finishAdActivity(int resultCode) {
		if(mPlayer != null) {
			mPlayer.destroy();
		}
		Intent intent = new Intent();
		intent.putExtra(DawinAdActivity.KEY_RESULT_CODE, resultCode);
		intent.putExtra("xml", mXml);
		setResult(RESULT_OK, intent);
		finish();
	}
	
	@Override
	public void onBackPressed() {
		mPlayer.onBackPressed();
		super.onBackPressed();
	}
	
	
	
}
